
package xj.util.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

/**
 * Splash screen. 
 */

public class SplashScreen extends JWindow {

  private static final SplashScreen splash = new SplashScreen(); 
  private JProgressBar progress;
  private JPanel pane; 
  
  private SplashScreen(){}
    
  public static SplashScreen getSplashScreen(){
    return splash;
  }
  public void startSplash(String text, ImageIcon icon){
    pane = new JPanel(new BorderLayout());
	  	
    JLabel splashBackground = new JLabel(icon);
    splashBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
    
    pane.add(splashBackground,BorderLayout.CENTER);
    
    progress = new JProgressBar(0, 100);
    progress.setStringPainted(true);
    progress.setBackground(Color.WHITE);
    //progress.setForeground(Color.GREEN);
    progress.setForeground(new Color(255, 0, 255));
    
    progress.setFont(new Font("Monospaced", Font.BOLD, 8));
    
    pane.add(progress, BorderLayout.SOUTH);
    getContentPane().add(pane);
    
    pack();
    
    Utilities.centerComponent(this);
    Utilities.setCursorOnWait(this, true);
    setVisible(true);
    setProgress(0);
    setText("Loading...");

    Dimension d = pane.getSize();
    pane.paintImmediately(0, 0, d.width, d.height); 
    d = progress.getSize();
    progress.paintImmediately(0, 0, d.width, d.height); 
  }

  /**
   * Sets the current text of the progress bar but
   * only in the case the loading of classes is ended.
   */
  public void setText(String text) {
    progress.setString(text);

    Dimension d = progress.getSize();
    progress.paintImmediately(0, 0, d.width, d.height); 
  }

  /**
   * Sets the current progress value of the progress bar but
   * only in the case the loading of classes is ended.
   */
  public void setProgress(int percent) {
    progress.setValue(percent);

    Dimension d = progress.getSize();
    progress.paintImmediately(0, 0, d.width, d.height); 
  }

}

